<?php
namespace Home\Controller;

use Home\Model\Admin;

use Think\Controller;

class RoomController extends BaseController
{
    public function index()
    {
        //var_dump($_GET);
        if(!empty($_GET['name'])){
            $this->assign('xname',$_GET['name']);
        }
        $id = $_SESSION['home']['id'];
        $room = M('room');
        $data['name'] = array('like', "%{$_GET['name']}%");
        $total = $room->where($data, ['user_id' => $id])->count();

        $page = new \Think\Page($total, 10);
        $page->setConfig('header', '共页数');
        $page->setConfig('prev', '上一页');
        $page->setConfig('next', '下一页');
        $page_str = $page->show();


        //var_dump($data);
        $list = $room->where($data, ['user_id' => $id])->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();

        $this->assign('page', $page);
        $this->assign('list', $list);
        $this->assign('active','room');
        $this->display('index');
    }

    public function adda()
    {

        $id = $_SESSION['home']['id'];
        //var_dump($_POST);
        /* if($_POST['name']==''){
             echo 2222;
         }*/
        if(!empty($_POST)){
            if(empty($_POST['name'])){
                session('message','内容不能为空');
            }else{
                $data['user_id'] = $id;
                $data['name'] = $_POST['name'];
                $Model = M('room');
                $res = $Model->add($data);

                if ($res) {
                    session('message','添加成功');
                    $this->redirect('index');
                    return;
                }else{
                    session('message','添加失败');
                }

            }
        }

       /* if (!$_POST['name'] == '') {
            $data['user_id'] = $id;
            $data['name'] = $_POST['name'];
            $Model = M('room');
            $res = $Model->add($data);

            if ($res) {
                $this->redirect('index');
                return;
            }

        }*/
       // $this->assign('message', '放映厅不能为空');
        $this->display('add');

    }

    public function update()
    {
        $id = $_GET['id'];
        $Model = M('room');
        $list = $Model->where(['id' => $id])->find();
        if (!empty($_POST)) {
            if (empty($_POST['name'])) {
                session('message', '内容不能为空');
            } else {
                $data['name'] = $_POST['name'];
                //$data['updated_at']=time();
                //var_dump($list);exit;
                $res  = $Model->where(['id' => $id])->save($data);
                if ($res) {
                    session('message', '修改成功');
                    $this->redirect('index');
                    retrun;
                } else {
                    session('message', '我猜你并没有做出修改');
                }
            }

        }
        $this->assign('list',$list);
        $this->display('update');
    }


}